package com.hrbxlh.virtualization.controller;

import com.hrbxlh.virtualization.param.ClipboardParam;
import com.hrbxlh.virtualization.param.DesktopShareParam;
import com.hrbxlh.virtualization.result.Result;
import com.hrbxlh.virtualization.service.ContainerService;
import com.hrbxlh.virtualization.service.DesktopService;
import com.hrbxlh.virtualization.util.ObjectUtil;
import com.hrbxlh.virtualization.util.SessionUtil;
import com.hrbxlh.virtualization.vo.DesktopVO;
import com.hrbxlh.virtualization.vo.UserListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/desktop")
public class DesktopController {

    @Autowired
    private DesktopService desktopService;

    @Autowired
    private ContainerService containerService;

    private ThreadLocal<Integer> retryCount = ThreadLocal.withInitial(() -> 0);

    /**
     * 获取实验桌面访问地址
     *
     * @param courseId  课程id
     * @param chapterId 章节id
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Result<DesktopVO> queryDesktop(Integer courseId, Integer chapterId) {
        DesktopVO desktopVO;
        try {
            desktopVO = desktopService.queryDesktop(SessionUtil.getStuorteaId(), courseId, chapterId);
        } catch (Exception e) {
            containerService.deleteContainerByCourse(SessionUtil.getStuorteaId(), courseId, chapterId);
            if (retryCount.get() == 0) {
                retryCount.set(retryCount.get() + 1);
                return queryDesktop(courseId, chapterId);
            } else {
                throw e;
            }
        } finally {
            retryCount.remove();
        }
        return Result.success(desktopVO);
    }

    /**
     * 替换桌面
     *
     * @param courseId  课程id
     * @param chapterId 章节id
     */
    @RequestMapping(value = "/replace", method = RequestMethod.GET)
    public Result<DesktopVO> replaceContainer(Integer courseId, Integer chapterId) {
        DesktopVO desktopVO = desktopService.replaceDesktop(SessionUtil.getStuorteaId(), courseId, chapterId);
        return Result.success(desktopVO);
    }

    /**
     * 下载文件
     *
     * @param courseId  课程Id
     * @param chapterId 章节Id
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void downloadFile(Integer courseId, Integer chapterId) {
        desktopService.downloadFile(SessionUtil.getStuorteaId(), courseId, chapterId);
    }

    /**
     * 剪贴板
     */
    @RequestMapping(value = "/clipboard", method = RequestMethod.POST)
    public Result clipboard(@RequestBody @Valid ClipboardParam clipboardParam) {
        desktopService.clipboard(clipboardParam);
        return Result.success();
    }

    /**
     *上传文件
     */
    @RequestMapping("/{containerId}/ftp")
    public Result uploadFile(@PathVariable("containerId") Long containerId,
                             MultipartFile file) throws IOException {
        desktopService.upload(containerId,file);
        return Result.success();
    }
//    /**
//     * ftp
//     */
//    @RequestMapping(value = "/ftp", method = RequestMethod.GET)
//    public Result<Map<String, Object>> ftp(Long containerId) {
//        String ftpUrl = desktopService.ftp(containerId);
//        return Result.success(ObjectUtil.build().addObject("ftp", ftpUrl).map());
//    }

    /**
     * 查询在线用户
     *
     * @param chapterId 章节Id
     */
    @RequestMapping(value = "/onlineUser", method = RequestMethod.GET)
    public Result<UserListVO> listOnlineUsers(Integer courseId, Integer chapterId) {
        UserListVO member = desktopService.listOnlineUsers(courseId, chapterId, SessionUtil.getStuorteaId());
        return Result.success(member);
    }

    /**
     * 分享实验环境
     */
    @RequestMapping(value = "/share", method = RequestMethod.POST)
    public Result shareDesktop(@RequestBody DesktopShareParam desktopShareParam) {
        desktopService.shareContainer(desktopShareParam, SessionUtil.getStuorteaId());
        return Result.success();
    }

    /**
     * 关闭分享实验环境
     *
     * @param containerId 容器Id
     */
    @RequestMapping(value = "/unshare", method = RequestMethod.GET)
    public Result unshareDesktop(Long containerId) {
        desktopService.unshareContainer(containerId, SessionUtil.getStuorteaId());
        return Result.success();
    }
}
